/*
 * File: calculator.c
 * Author:  Yin Lijun <yinlijun2004@gmail.com>
 * Brief:   calculator interface.
 *
 * Copyright (c) 2010 - 2012  Yin Lijun <yinlijun2004@gmail.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2012-03-12 Yin Lijun <yinlijun2004@gmail.com> created
 *
 */

#include "calculator.h"
#include "stack_from_darray.h"

struct _Calculator
{
	Stack* data_stack;
	Stack* op_stack;
	CalcIface* ci;	
};

enum _ToObject
{
	TO_DATA,
	TO_OP
};

typedef enum _ToObject ToObject;

Calculator* calc_create(CalcIface* ci)
{
	return_if_fail_val(ci != NULL 
		&& ci->data_dest_func != NULL 
		&& ci->op_dest_func != NULL
		&& ci->op_prior_cmp_func != NULL
		&& ci->need_bt_func != NULL
		&& ci->calc_func != NULL
		&& ci->get_oprs_num_func != NULL,
		NULL);
	Calculator* thiz = (Calculator *)malloc(sizeof(Calculator));
	return_if_fail_val(thiz != NULL, NULL);

	thiz->data_stack = stack_create(ci->data_dest_func);
	if(thiz->data_stack == NULL)
	{
		free(thiz);
		return NULL;
	}
	
	thiz->op_stack = stack_create(ci->op_dest_func);
	if(thiz->op_stack == NULL)
	{
		stack_destroy(thiz->data_stack);
		free(thiz);
		return NULL;
	}
	
	thiz->ci = ci;	
	
	return thiz;
}

static Stack* internal_get_stack(Calculator* thiz, ToObject to_what)
{
	int to_data = to_what == TO_DATA ? 1 : 0;	
	Stack* stack = to_data ? thiz->data_stack : thiz->op_stack;	
	
	return stack;
}

static Ret calc_internal_foreach(Calculator* thiz, ToObject to_what, VisitFunc visit_func, void* ctx)
{
	Stack* stack = internal_get_stack(thiz, to_what);

	return stack_foreach(stack, visit_func, ctx);
}

Ret calc_foreach_data(Calculator* thiz, VisitFunc data_visit_func, void* ctx)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);

	return calc_internal_foreach(thiz, TO_DATA, data_visit_func, ctx);
}

Ret calc_foreach_op(Calculator* thiz, VisitFunc op_visit_func, void* ctx)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);

	return calc_internal_foreach(thiz, TO_OP, op_visit_func, ctx);
}

static Ret calc_internal_push_item(Calculator* thiz, ToObject to_what, void* data)
{
	Stack* stack = internal_get_stack(thiz, to_what);
	
	return stack_push_data(stack, data);
}

Ret calc_on_data(Calculator* thiz, void* data)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);
	
	return calc_internal_push_item(thiz, TO_DATA, data);
}

static Ret internal_free_mem(Calculator* thiz, int num, void** data, ToObject to_what)
{
	DestoryFunc free_func = (to_what == TO_OP)
			? thiz->ci->op_dest_func
			: thiz->ci->data_dest_func;

	int i = 0;
	while(i < num)
	{
		free_func(data[i]);
		i++;
	}
	
	return RET_OK;
}

static Ret internal_calculation(Calculator* thiz, void* last_op)
{
	int num = thiz->ci->get_oprs_num_func(last_op);
	if (num > stack_get_length(thiz->data_stack))
	{
		printf("Not enough operands! somthing must be wrong!\n");
	}
	void** data = (void**)malloc(num * sizeof(void *));
	return_if_fail_val(data != NULL, RET_OVERFLOW);
	int i = 0;
	while(i < num)
	{
		data[i++] = stack_pop_data(thiz->data_stack);
	}
	
	if(num > 0)
	{
		void* calc_data = thiz->ci->calc_func(last_op, data, num);
		stack_push_data(thiz->data_stack, calc_data);
	}
	internal_free_mem(thiz, num, data, TO_DATA);
	free(data);

	return RET_OK;
}

Ret calc_on_operator(Calculator* thiz, void* cur_op)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);
	
	void* last_op = NULL;
	stack_get_top_data(thiz->op_stack, &last_op);
	/* Compare the priority between current operator and
	 * the last stored operator,
	 * if returns higher(>0), get the number of operands
	 * and do calculation
	 * if lower, push the current operator into stack.
	 */
	int need_bt = thiz->ci->need_bt_func(cur_op);
	if (last_op != NULL && thiz->ci->op_prior_cmp_func(last_op, cur_op) > 0)
	{
		last_op = stack_pop_data(thiz->op_stack);
		internal_calculation(thiz, last_op);
		internal_free_mem(thiz, 1, &last_op, TO_OP);
		if (need_bt)
		{
			void* bt_op = stack_pop_data(thiz->op_stack);
			internal_free_mem(thiz, 1, &bt_op, TO_OP);
			internal_free_mem(thiz, 1, &cur_op, TO_OP);
			
			return RET_OK;
		}
	}
	
	calc_internal_push_item(thiz, TO_OP, cur_op);

	return RET_OK;
}

Ret calc_on_end(Calculator* thiz)
{
	void* op = stack_pop_data(thiz->op_stack);
	while(op != NULL)
	{	
		Ret ret = internal_calculation(thiz, op);
		internal_free_mem(thiz, 1, &op, TO_OP);
		if (ret == RET_OK)	
		{
			/* If current calculation is OK, 
			 * go on! 
			 * else return ERROR*/
			op = stack_pop_data(thiz->op_stack);
			continue;
		}

		return ret;
	}

	return RET_OK;
}

void* calc_get_result(Calculator* thiz)
{
	if (stack_get_length(thiz->data_stack) != 1
		&& stack_get_length(thiz->op_stack) != 0);
	{
		return stack_pop_data(thiz->data_stack);
	}

	return NULL;
}

Ret calc_destroy(Calculator* thiz)
{
	stack_destroy(thiz->data_stack);
	stack_destroy(thiz->op_stack);
	free(thiz);

	return RET_OK;
}
